#!/bin/bash
#
# Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Snapshots - Kernel tracing"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../..
EVENT_NAME="sched_switch"
SESSION_NAME=""
CHANNEL_NAME="snapchan"

TRACE_PATH=$(mktemp -d -t tmp.test_snapshots_kernel_trace_path.XXXXXX)

NUM_TESTS=2061

source $TESTDIR/utils/utils.sh

function test_kernel_local_snapshot ()
{
	diag "Test local kernel snapshots"
	create_lttng_session_no_output $SESSION_NAME
	enable_lttng_mmap_overwrite_kernel_channel $SESSION_NAME $CHANNEL_NAME
	lttng_enable_kernel_event $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
	start_lttng_tracing_ok $SESSION_NAME
	lttng_snapshot_add_output_ok $SESSION_NAME file://$TRACE_PATH
	lttng_snapshot_record $SESSION_NAME
	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	# Validate test
	validate_trace_path_kernel_snapshot "$TRACE_PATH" "" "snapshot-1" 0 ""
	validate_trace $EVENT_NAME $TRACE_PATH/
	if [ $? -eq 0 ]; then
		# Only delete if successful
		rm -rf $TRACE_PATH
	fi
}

function test_kernel_local_snapshot_after_stop ()
{
	diag "Test local kernel snapshots after stop"
	create_lttng_session_no_output $SESSION_NAME
	enable_lttng_mmap_overwrite_kernel_channel $SESSION_NAME $CHANNEL_NAME
	lttng_enable_kernel_event $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
	start_lttng_tracing_ok $SESSION_NAME
	stop_lttng_tracing_ok $SESSION_NAME
	lttng_snapshot_add_output_ok $SESSION_NAME file://$TRACE_PATH
	lttng_snapshot_record $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	# Validate test
	validate_trace $EVENT_NAME $TRACE_PATH/
	if [ $? -eq 0 ]; then
		# Only delete if successful
		rm -rf $TRACE_PATH
	fi
}

function test_kernel_local_snapshot_append_to_metadata ()
{
	local EVENT1=sched_switch
	local EVENT2=sched_process_exit

	diag "Test local kernel snapshots with one event $EVENT1"
	create_lttng_session_no_output $SESSION_NAME
	enable_lttng_mmap_overwrite_kernel_channel $SESSION_NAME $CHANNEL_NAME
	lttng_enable_kernel_event $SESSION_NAME $EVENT1 $CHANNEL_NAME
	start_lttng_tracing_ok $SESSION_NAME
	lttng_snapshot_add_output_ok $SESSION_NAME file://$TRACE_PATH

	# first snapshot with only 1 event
	lttng_snapshot_record $SESSION_NAME
	validate_trace $EVENT_NAME $TRACE_PATH/
	if [ $? -eq 0 ]; then
		# Only delete if successful
		rm -rf $TRACE_PATH
	fi

	diag "Adding event $EVENT2"
	# second snapshot with 2 events
	lttng_enable_kernel_event $SESSION_NAME $EVENT2 $CHANNEL_NAME
	rm -rf $TRACE_PATH/snapshot/* 2>/dev/null
	lttng_snapshot_record $SESSION_NAME
	validate_trace "${EVENT1},${EVENT2}" $TRACE_PATH/
	if [ $? -eq 0 ]; then
		# Only delete if successful
		rm -rf $TRACE_PATH
	fi

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME
}

function generate_filter_events_cpu0()
{
	# Generate many events (fill buffers) on a single CPU
	taskset --cpu-list "$(get_any_available_cpu)" /bin/echo -n "$1" > /proc/lttng-test-filter-event 2> /dev/null
}

function test_kernel_local_snapshot_discard ()
{
	diag "Test local kernel snapshots with small discard buffers"

	local event_name="lttng_test_filter_event"

	modprobe lttng-test
	create_lttng_session_no_output $SESSION_NAME
	enable_lttng_mmap_discard_small_kernel_channel $SESSION_NAME \
			$CHANNEL_NAME
	enable_kernel_lttng_event_ok $SESSION_NAME $event_name $CHANNEL_NAME
	start_lttng_tracing_ok $SESSION_NAME
	lttng_snapshot_add_output_ok $SESSION_NAME file://$TRACE_PATH

	generate_filter_events_cpu0 10000

	# Take first snapshot, remember first line.
	lttng_snapshot_record $SESSION_NAME
	FIRST_LINE="$(trace_first_line $TRACE_PATH/)"
	diag "First line (1st snapshot): $FIRST_LINE"

	rm -rf $TRACE_PATH

	# Fill CPU 0's buffer and overwrite previous contents
	generate_filter_events_cpu0 10000

	# Take 2nd snapshot, compare first line. In discard mode, they
	# should still be the same.
	lttng_snapshot_record $SESSION_NAME
	FIRST_LINE_2="$(trace_first_line $TRACE_PATH/)"
	diag "First line (2nd snapshot): $FIRST_LINE"
	rm -rf $TRACE_PATH

	if [ x"$FIRST_LINE" != x"$FIRST_LINE_2" ]; then
		fail "First snapshot event do not match"
	else
		pass "First snapshot event match"
	fi

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME
	modprobe --remove lttng-test
}

function test_kernel_local_snapshot_overwrite_small_buffers ()
{
	diag "Test local kernel snapshot with small overwrite buffers"

	local event_name="lttng_test_filter_event"

	modprobe lttng-test
	create_lttng_session_no_output $SESSION_NAME
	enable_lttng_mmap_overwrite_small_kernel_channel $SESSION_NAME \
			$CHANNEL_NAME
	enable_kernel_lttng_event_ok $SESSION_NAME $event_name $CHANNEL_NAME
	start_lttng_tracing_ok $SESSION_NAME
	lttng_snapshot_add_output_ok $SESSION_NAME file://$TRACE_PATH

	generate_filter_events_cpu0 10000

	# Take first snapshot, remember first line.
	lttng_snapshot_record $SESSION_NAME
	FIRST_LINE="$(trace_first_line $TRACE_PATH/)"
	diag "First line (1st snapshot): $FIRST_LINE"

	rm -rf $TRACE_PATH

	# Fill CPU 0's buffer and overwrite previous contents
	generate_filter_events_cpu0 10000

	# Take 2nd snapshot, compare first line. In overwrite mode, they
	# WILL be different.
	lttng_snapshot_record $SESSION_NAME
	FIRST_LINE_2="$(trace_first_line $TRACE_PATH/)"
	diag "First line (2nd snapshot): $FIRST_LINE_2"
	rm -rf $TRACE_PATH

	if [ x"$FIRST_LINE" != x"$FIRST_LINE_2" ]; then
		pass "First snapshot event do not match"
	else
		fail "First snapshot event match"
	fi

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME
	modprobe --remove lttng-test
}

function test_kernel_1000_local_snapshots ()
{
	NB_SNAP=1000

	diag "Test local kernel snapshots"
	create_lttng_session_no_output $SESSION_NAME
	enable_lttng_mmap_overwrite_kernel_channel $SESSION_NAME $CHANNEL_NAME
	lttng_enable_kernel_event $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
	start_lttng_tracing_ok $SESSION_NAME
	lttng_snapshot_add_output_ok $SESSION_NAME file://$TRACE_PATH
	for i in $(seq 1 $NB_SNAP); do
		diag "Snapshot $i/$NB_SNAP"
		rm -rf $TRACE_PATH/snapshot/* 2>/dev/null
		lttng_snapshot_record $SESSION_NAME
		# Validate test
		validate_trace $EVENT_NAME $TRACE_PATH/
		if [ $? -eq 0 ]; then
			# Only delete if successful
			rm -rf $TRACE_PATH
		else
			break
		fi
	done
	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME
}

plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

bail_out_if_no_babeltrace

check_skip_kernel_test "$NUM_TESTS" "Skipping all tests." ||
{

	validate_lttng_modules_present

	start_lttng_sessiond

	#tests=( test_kernel_1000_local_snapshots )
	tests=( test_kernel_local_snapshot
		test_kernel_local_snapshot_after_stop
		test_kernel_local_snapshot_append_to_metadata
		test_kernel_local_snapshot_discard
		test_kernel_local_snapshot_overwrite_small_buffers
		test_kernel_1000_local_snapshots
	)

	for fct_test in ${tests[@]};
	do
		SESSION_NAME=$(randstring 16 0)
		${fct_test}

	done

	stop_lttng_sessiond
}
